package com.shj.controller;

import com.alibaba.fastjson.JSONObject;
import com.shj.entity.SysChronicPaper;
import com.shj.entity.SysPerson;
import com.shj.service.SysPaperService;
import com.shj.service.SysPersonService;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author songhuajian
 * @Description: TODO
 * @date 2020/6/17 15:39
 **/
@Controller
@RequestMapping("/biz/paper")
public class SysPaperController {
    @Autowired
    private SysPaperService sysPaperService;
    @Autowired
    private SysPersonService sysPersonService;

    @RequestMapping("/list")
    public String findAll(Model model) {
        List<SysChronicPaper> list = sysPaperService.findAll();
        model.addAttribute("list", list);
        return "paper/paper_list";
    }

    @RequestMapping("/findPerson")
    public String findPersonList(Model model, String perName) {
        if ("".equals(perName)) {
            perName = null;
        }
        List<SysPerson> list = sysPersonService.findAll(perName);
        model.addAttribute("list", list);
        return "paper/person_list";
    }

    @RequestMapping("/openAdd")
    public String openAdd(Model model, String code) {
        model.addAttribute("code", code);
        return "paper/paper_add";
    }

    @RequestMapping("/add")
    public String add(Model model, SysChronicPaper sysChronicPaper) {
        sysPaperService.add(sysChronicPaper);
        return findAll(model);
    }

    @RequestMapping("/openEdit")
    public String openEdit(Model model, Integer id) {
        SysChronicPaper paper = sysPaperService.findById(id);
        model.addAttribute("paper", paper);
        return "paper/paper_edit";
    }

    @RequestMapping("/edit")
    public String edit(Model model, SysChronicPaper sysChronicPaper) {
        sysPaperService.edit(sysChronicPaper);
        return findAll(model);
    }

    @RequestMapping("/delete")
    public String delete(Model model, Integer id) {
        sysPaperService.delete(id);
        return findAll(model);
    }

    @RequestMapping("/equalChName")
    public void equalChName(String perCode, String chName, HttpServletResponse response) throws IOException {
        SysChronicPaper sysChronicPaper = sysPaperService.equalChName(perCode, chName);
        String msg = "信息一致";
        if (sysChronicPaper == null) {
            msg = "与慢性病证信息不一致";
        }
        JSONObject json = new JSONObject();
        json.put("msg", msg);
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json; charset=utf-8");
        PrintWriter out = null;
        out = response.getWriter();
        out.write(json.toString());
        out.close();
    }
}
